package com.gao.security.verificationcode.common.vo;

import com.gao.security.verificationcode.common.enums.ResponseCode;
import lombok.Data;

import java.io.Serializable;


/**
 * <p>
 * 返回前端数据VO
 * </p>
 *
 * @author GaoShuangWen
 */
@Data
public class BaseVo<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    /**
     * 状态码
     */
    private Integer code;
    /**
     * 返回体
     */
    private T data;
    /**
     * 描述信息
     */
    private String msg;

    public BaseVo() {

    }

    public BaseVo(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    /**
     * 用于成功，无data返回体，比如更新删除
     *
     * @return
     */
    public static <T> BaseVo<T> success() {
        BaseVo<T> baseVo = new BaseVo<>();
        baseVo.setResponseCode(ResponseCode.SUCCESS);
        return baseVo;
    }

    public static <T> BaseVo<T> success(T data) {
        BaseVo<T> baseVo = new BaseVo<>();
        baseVo.setResponseCode(ResponseCode.SUCCESS);
        baseVo.setData(data);
        return baseVo;
    }

    public static <T> BaseVo<T> failure(ResponseCode responseCode) {
        BaseVo<T> baseVo = new BaseVo<>();
        baseVo.setResponseCode(responseCode);
        return baseVo;
    }

    public static <T> BaseVo<T> failure(ResponseCode responseCode, T data) {
        BaseVo<T> baseVo = new BaseVo<>();
        baseVo.setResponseCode(responseCode);
        baseVo.setData(data);
        return baseVo;
    }

    public void setResponseCode(ResponseCode responseCode) {
        this.code = responseCode.code();
        this.msg = responseCode.message();
    }

}
